package com.qdu.controller;


import com.qdu.commons.Result;
import com.qdu.entity.User;
import com.qdu.service.impl.PhotoServiceImpl;
import com.qdu.service.impl.UserServiceImpl;
import com.qdu.util.AliyunUtil;
import com.qdu.util.BlogProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ellery2018
 * @since 2022-03-09
 */
@RestController
@RequestMapping("/photo")
public class PhotoController {
    @Autowired
    PhotoServiceImpl photoService;

    //  @RequestMapping(value = "/")
//    @PostMapping("/uploadFile")
//    public void uploadFile(HttpSession session, MultipartFile myfile,@RequestParam("mypose") String mypose,@RequestParam("userid") Integer userid,@RequestParam("routeid") Integer routeid ) throws  IOException {
//
//        System.out.println("!!!!!!!!@#@$!#@$!@$@!$@$$@@@@"+myfile.getOriginalFilename());
//        // 1.文件名称
//        String oldFileName = myfile.getOriginalFilename(); //获取上传文件的原名
//        System.out.println("#%%%%%%%%%"+userid +mypose+routeid);
//        // 2.存储图片的物理路径
//        String file_path = session.getServletContext().getRealPath("upload");
//        session.getAttribute(mypose);
//        // 3.上传图片
//        if(myfile!=null && oldFileName!=null && oldFileName.length()>0){
//            // 4.新的图片名称
//            String newFileName = UUID.randomUUID() + oldFileName.substring(oldFileName.lastIndexOf("."));
//            // 5.新图片
//            File newFile = new File(file_path+"/"+newFileName);
//            System.out.println(file_path+"/"+newFileName);
//            // 6.将内存中的数据写入磁盘
//            myfile.transferTo(newFile);
//            // 7.将文件名字保存到数据库，前端读取的时候会用到。或者可以把图片名称直接传给前端做一个图片的回显，这里大家自己操作。
//
//            System.out.println(newFileName);
//            photoService.insertphoto(mypose, userid,routeid,file_path+"/"+newFileName);
//        }else{
//        }
//    }

    @Autowired
    private BlogProperties blogProperties;

    @PostMapping("/uploadFile2")
    public void uploadFile(HttpSession session, MultipartFile myfile,@RequestParam("mypose") String mypose,@RequestParam("userid") Integer userid,@RequestParam("routeid") Integer routeid ) throws  IOException {



        String tem=AliyunUtil.uploadFileToOss(myfile, blogProperties.getAliyun());
        photoService.insertphoto(mypose, userid,routeid,tem);

    }
    //用户登录名称 ellerylrh@1701004671156018.onaliyun.com
    //AccessKey ID LTAI5t7v2iZbVx2627GoLLeH
    //AccessKey Secret 8Ay6aPmSn5v3XTWLOHAMBl3vCaX4q3

}

